//
//  TodayLive_Cell.m
//  penzer
//
//  Created by caiyc on 2018/10/3.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "TodayLive_Cell.h"
#import "OYCountDownManager.h"
#define kCountDownNotification @"cutdown"
@implementation TodayLive_Cell{
    NSInteger countDwonTime;
    NSInteger endtime;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
//    self.contentView.layer.cornerRadius = 5;
//    self.contentView.layer.masksToBounds = 1;
    self.time_BottomView.layer.cornerRadius = 12;
    self.time_BottomView.layer.masksToBounds = 1;
    
    self.time_BottomView1.layer.cornerRadius = 12;
    self.time_BottomView1.layer.masksToBounds = 1;
    
    self.time_BottomView2.layer.cornerRadius = 12;
    self.time_BottomView2.layer.masksToBounds = 1;
    
    self.contact_Btn.layer.cornerRadius = 15;
    self.contact_Btn.layer.masksToBounds = 1;
    
    self.status_labss.hidden = 1;
    self.logo.layer.cornerRadius = 10;
    self.logo.layer.masksToBounds = 1;
    
    self.conerView.layer.cornerRadius = 5;
    self.conerView.layer.masksToBounds = 1;
    // Initialization code
}
-(void)setPriceAttributeText{
    
}
-(void)bindData:(NSDictionary *)dic andNowTime:(NSString *)nowtime{
    self.live_Data = dic;
    
    NSDictionary *goodDic = dic[@"goods_list"][0];
    
    NSURL *goodImg = [NSURL URLWithString:goodDic[@"thumb"]];
    [self.goods_Img sd_setImageWithURL:goodImg];
    self.goods_Num.text = [NSString stringWithFormat:@"%lu件商品",[dic[@"goods_list"]count]];
    self.goods_Name.text = goodDic[@"title"];
    NSString *price_text = [NSString stringWithFormat:@"¥%@",goodDic[@"buy_price"]];
    NSDictionary *attrDict1 = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                                NSForegroundColorAttributeName:PRICE_COLOR };
    NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [price_text substringWithRange: NSMakeRange(0, price_text.length)] attributes: attrDict1];
    
    NSString *price_text1 = @"直播价  ";
   // NSString *price_Text2 = [NSString stringWithFormat:@"%@",];
    NSDictionary *attrDict2 = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:PRICE_COLOR };
    NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: price_text1 attributes: attrDict2];
    NSString *old_price = [NSString stringWithFormat:@"  ¥%@",goodDic[@"price"]];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColor grayColor]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[old_price substringWithRange: NSMakeRange(2, old_price.length-2)] attributes:attribtDic];
    
    NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];
    [attributedStr03 appendAttributedString:attrStr3];
    [attributedStr03 appendAttributedString:attribtStr];
    self.price.attributedText = attributedStr03;
    self.max_Guest.text = [NSString stringWithFormat:@"%@人",dic[@"member_max"]];
    self.live_saleType.text = dic[@"live_type_cn"];
    NSURL *logo = [NSURL URLWithString:dic[@"logo"]];
    [self.logo sd_setImageWithURL:logo];
    self.store_title.text = dic[@"title"];
    countDwonTime = [self.live_Data[@"start_time"] integerValue] -  [nowtime integerValue];
    endtime =  [self.live_Data[@"end_time"] integerValue] -  [nowtime integerValue];
    if(countDwonTime>=0){
    }else{
         self.status_labss.hidden = 0;
    if(endtime>0)
    {
       self.status_labss.text = @"已到直播开始时间";
    }
    else{
     self.status_labss.text = @"不在直播时段";
    }
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
    }
    return self;
}
// xib创建
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
    }
    return self;
}
- (void)countDownNotification {
    /// 计算倒计时
    // NSInteger countDown = [self.live_Data[@"start_time"] integerValue] -  [self.live_Data[@"now_time"] integerValue];
    if (countDwonTime <= 0) {
        // 倒计时结束时回调
        // xxxx(使用代理或block)
        self.status_labss.hidden = 0;
        self.status_labss.text = @"已到直播开始时间";
        return;
    }
    [self getMMSSFromSS:countDwonTime];
    countDwonTime--;

}
-(NSString *)getMMSSFromSS:(NSInteger )totalTime{
    NSInteger seconds = totalTime;

    NSString *str_hour =  [NSString stringWithFormat:@"%ld",seconds/3600];
    if(str_hour.length>1){
        self.h1_time.text = [str_hour substringWithRange:NSMakeRange(0, 1)];
        self.h2_time.text = [str_hour substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.h2_time.text = str_hour;
    }
    NSString *str_minute = [NSString stringWithFormat:@"%ld",(seconds%3600)/60];
    if(str_minute.length>1){
        self.m1_time.text = [str_minute substringWithRange:NSMakeRange(0, 1)];
        self.m2_time.text =  [str_minute substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.m2_time.text = str_minute;
    }
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    if(str_second.length>1){
        self.s1_time.text = [str_second substringWithRange:NSMakeRange(0, 1)];
        self.s2_time.text =  [str_second substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.s1_time.text = @"0";
        self.s2_time.text = str_second;
    }
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@分钟%@秒",str_minute,str_second];
    
   // NSLog(@"format_time : %@",format_time);
    
    return format_time;
}
- (IBAction)enterLive:(UIButton *)sender {
    self.enter();
}
- (IBAction)contact:(UIButton *)sender {
    self.call();
}
@end
